//
//  ByBetRecordDetailVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/8.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBetRecordDetailVC.h"
#import "ByBetRecordTitleCell.h"
#import "ByBetRecordInfoCell.h"
#import "ByBetRecordromptCell.h"
#import "ByBetCirclesView.h"
#import "ByTabBarVC.h"
#import "BySuperLottoVC.h"
#import "ByDoubleBallVC.h"


@interface ByBetRecordDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ByNetModel *infoModel;

@end

@implementation ByBetRecordDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"投注详情";
    [self loadData];
    
   
    // Do any additional setup after loading the view.
}

- (void)loadData{
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.ID = self.netModel.ID;
    [ByApiOther betRecoreListInfo:netModel success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
         [self initViews];
        SLog(@"投注详情 = %@", result.data);
        NSDictionary *dictionary = result.data[@"data"];
        self.infoModel = [[ByNetModel alloc] initContentWithDic:dictionary[@"info"]];
        
        
        
        NSMutableArray *totalAry = [NSMutableArray arrayWithCapacity:1];
        
        
        
        for (NSDictionary *betDic in dictionary[@"bet"]) {
            
            NSString *red_ball = betDic[@"red_ball"];
            NSString *red_ball_drag = betDic[@"red_ball_drag"];
            
            NSString *blue_ball = betDic[@"blue_ball"];
            NSString *blue_ball_drag = betDic[@"blue_ball_drag"];
            
            NSArray *redBallAry1 = [red_ball componentsSeparatedByString:@","];
            NSArray *redBallAry2 = [red_ball_drag componentsSeparatedByString:@","];
            
            NSArray *blueBallAry1 = [blue_ball componentsSeparatedByString:@","];
            NSArray *blueBallAry2 = [blue_ball_drag componentsSeparatedByString:@","];
            
        //普通投注
        if (red_ball.length > 0 && blue_ball.length > 0 && red_ball_drag.length == 0 && blue_ball_drag.length ==0) {
            
    
            NSMutableArray *redArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:redBallAry1 WithColor:BY_THEME_COLOR];
            NSMutableArray *blueArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:blueBallAry1 WithColor:BY_Blue_COLOR];
            NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
            [dataArray addObject:redArray];
            [dataArray addObject:blueArray];
            [totalAry addObject:dataArray];
         
        //双色球胆拖投注
        }else if (red_ball.length > 0 && blue_ball.length > 0 && red_ball_drag.length >  0 && blue_ball_drag.length ==0){
            NSMutableArray *redArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:redBallAry1 WithColor:BY_THEME_COLOR];
            NSMutableArray *redArray1 = [ByLotteryAlgorithmHandle getModelsArrayWithArray:redBallAry2 WithColor:BY_THEME_COLOR];
            NSMutableArray *blueArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:blueBallAry1 WithColor:BY_Blue_COLOR];
            NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
            [dataArray addObject:redArray];
            [dataArray addObject:redArray1];
            [dataArray addObject:blueArray];
            [totalAry addObject:dataArray];
           
         //大乐透胆拖投注
        }else if (red_ball.length > 0 && blue_ball.length > 0 && red_ball_drag.length >  0 && blue_ball_drag.length > 0){
            
            NSMutableArray *redArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:redBallAry1 WithColor:BY_THEME_COLOR];
            NSMutableArray *redArray1 = [ByLotteryAlgorithmHandle getModelsArrayWithArray:redBallAry2 WithColor:BY_THEME_COLOR];
            NSMutableArray *blueArray = [ByLotteryAlgorithmHandle getModelsArrayWithArray:blueBallAry1 WithColor:BY_Blue_COLOR];
            NSMutableArray *blueArray1 = [ByLotteryAlgorithmHandle getModelsArrayWithArray:blueBallAry2 WithColor:BY_Blue_COLOR];
            NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
            [dataArray addObject:redArray];
            [dataArray addObject:redArray1];
            [dataArray addObject:blueArray];
            [dataArray addObject:blueArray1];
            [totalAry addObject:dataArray];
        }
    }
        self.infoModel.dataArray = [totalAry copy];
        [self.tableView reloadData];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}


-(void)initViews{
   
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(-49);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedRowHeight = 0;
    
    UIButton *betBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:betBtn];
    [betBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.bottom.equalTo(self.view).offset(0);
        }
        make.height.mas_equalTo(@49);
    }];
    betBtn.backgroundColor = BY_THEME_COLOR;
    [betBtn setTitle:@"继续投注" forState:UIControlStateNormal];
    [betBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [betBtn addTarget:self action:@selector(betAction:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)betAction:(UIButton *)btn{
    if (![BY_APPDELEGATE.supportTypeString containsString:[NSString stringWithFormat:@"%@", self.netModel.gamecate]]) {
        [ByCoreToolAlert showOnlyText:@"暂不支持"];
        return ;
    }
    //大乐透
    if (self.netModel.gamecate.integerValue == 2001) {
        BySuperLottoVC *vc = [[BySuperLottoVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
        //双色球
    }else if (self.netModel.gamecate.integerValue == 1001){
        ByDoubleBallVC *vc = [[ByDoubleBallVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        ByBetRecordTitleCell *cell = [ByBetRecordTitleCell cellWithTableView:tableView];
        cell.netModel = self.infoModel;
        return cell;
    }else if (indexPath.row == 1){
        
        ByBetRecordInfoCell *cell = [ByBetRecordInfoCell cellWithTableView:tableView];
        cell.netModel = self.infoModel;
        return cell;
        
    }else{
        
        ByBetRecordromptCell *cell = [ByBetRecordromptCell cellWithTableView:tableView];
        return cell;
    }
    
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 90;
    }else if (indexPath.row == 1){
        
        CGFloat top = 105;
        NSMutableArray *totalAry  = [self.infoModel.dataArray copy];
        for (int j = 0; j <  totalAry.count; j++) {
            
        NSMutableArray *dataArray = [totalAry objectAtIndex:j];
        for (int i = 0; i < dataArray.count; i++) {
            NSMutableArray *smallArray = [dataArray objectAtIndex:i];
            top = top + [ByBetCirclesView getHeightWithDataArray:smallArray];
        }
            
        }
        NSLog(@"top = %.f", top);
        return top + 180;
        
    }else{
        
        return 180;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
